package org.jeecg.modules.zhongche.plc.api;//package org.jeecg.modules.zhongche.plc.api;
//
//import cn.hutool.core.util.StrUtil;
//import lombok.Data;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.plc4x.java.api.PlcConnection;
//import org.apache.plc4x.java.api.PlcConnectionManager;
//import org.apache.plc4x.java.api.PlcDriverManager;
//import org.apache.plc4x.java.api.messages.PlcReadRequest;
//import org.jeecg.modules.zhongche.plc.api.PlcDataType;
//import org.jeecg.modules.zhongche.plc.api.PlcField;
//
//import java.lang.reflect.Field;
//
//@Data
//@Slf4j
//public class PlcSerializer {
//
//    private PlcConnector connector;
//
//    private PlcConnection plcConnection;
//
//    private   PlcReadRequest.Builder read;
//
//    public PlcSerializer(PlcConnector connector) {
//        this.connector = connector;
//
//        String connectionString = connector.getHost();
//
//        PlcConnectionManager plcConnectionManager = PlcDriverManager.getDefault().getConnectionManager();
//
//        if (null == plcConnectionManager) {
//            throw new RuntimeException("无法链接");
//        }
//        PlcConnection plcConnection = null;
//
//
//        try {
//            plcConnection = plcConnectionManager.getConnection(connectionString);
//
//            if (null == plcConnection) {
//                throw new RuntimeException("未获取到链接");
//            }
//
//            if (!plcConnection.getMetadata().isReadSupported()) {
//                log.error("This connection doesn't support reading.");
//                throw new RuntimeException("不支持读取数据");
//            }
//        } catch (Exception e) {
//            log.error("PLC链接失败{}", e.getMessage());
//        }
//
//        this.plcConnection = plcConnection;
//
//        if (plcConnection != null) {
//            PlcReadRequest.Builder read = plcConnection.readRequestBuilder();
//            this.read = read;
//        }
//
//
//    }
//
//
//    public String address( int dbNum ,String offset, PlcDataType type) {
//        return StrUtil.format("%DB{}.{}{}:{}", dbNum, type.getCode(), offset, type.getType());
//    }
//
//
//    <T> T dispense(Class<T> beanClass, int dbNum, int byteOffset){
//
//        PlcReadRequest.Builder read = plcConnection.readRequestBuilder();
//
//        for (Field field : beanClass.getFields()) {
//            if (null != field.getAnnotation(PlcField.class)) {
//                PlcField annotation = field.getAnnotation(PlcField.class);
//                PlcDataType dataType = annotation.dataType();
//                String offset = annotation.offset();
//                read.addTagAddress(field.getName(), address(dbNum,offset, dataType));
//            }
//        }
//
//    };
//}
